<template>
  <div id="addUserLabels">
    <el-dialog
      title="修改标签"
      :visible.sync="dialogForSort.isShow"
      width="500px"
      :before-close="labelHandleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
        @submit.native.prevent
      >
        <el-form-item label="显示排序" prop="sortNum">
          <el-input-number
            v-model="ruleForm.sortNum"
            size="mini"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="cancelLabel">取 消</el-button>
        <el-button
          type="success"
          :loading="loading"
          @click.prevent="confirmLabel('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sortContTag } from '@/api/appSetting/contentTag' // 接口
export default {
  name: 'AddUserLabels',
  msg: '标签分类管理-新增弹窗',
  props: ['dialogForSort'],
  data: function() {
    //校验排序
    var valiTagSort = (rule, value, callback) => {
      if (!value) {
        callback(new Error('数字不能为空'))
      }
      callback()
    }
    return {
      loading: false,
      ruleForm: {
        sortNum: ''
      },
      rules: {
        sortNum: [{ validator: valiTagSort, trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.cId = this.dialogForSort.params.cId
    this.total = this.dialogForSort.total
    this.ruleForm.sortNum = this.dialogForSort.params.sortNum
  },
  methods: {
    // 确认操纵
    confirmLabel(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.subForm()
        } else {
          return false
        }
      })
    },
    /* 确认提交 */
    subForm() {
      this.loading = true
      let params = {
        sortNum: this.ruleForm.sortNum
      }
      if (this.ruleForm.sortNum > this.total) {
        this.$message.error('置顶序号不能大于当前总数')
        this.loading = false
      } else {
        sortContTag(
          this.dialogForSort.id,
          this.cId,
          params,
          (response) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.loading = false
            this.$emit('refresh')
            this.cancelLabel()
          },
          (response) => {
            this.loading = false
            this.$message({
              message: response.message,
              type: 'warning'
            })
          }
        )
      }
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.dialogForSort.isShow = false
    }
  }
}
</script>
<style lang="less" scoped>
#addUserLabels {
  .form-box {
    height: 300px;
  }
  .form-box {
    padding-left: 100px;
  }
  .el-input {
    width: 220px;
  }
  .el-dialog {
    border-radius: 4px;
    .el-dialog__header {
      border-bottom: 1px solid rgba(225, 228, 233, 1);
    }
  }
  .el-dialog__footer {
    margin-top: 50px;
    padding: 10px;
    /*border-top: 1px solid #eeeeee;*/
  }
}
</style>
